/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.cn360.batch.BuildCn360PriceJob
   Module Description   :

   Date Created      : 2011/6/21
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.cn360.batch;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import com.csvreader.CsvWriter;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.thread.batch.AbstractRunJob;
import com.yaodian100.ec.cn360.remote.RemoteCn360ProductService;

/**
 * @author jeffma
 * 
 */
public class BuildCn360PriceJob extends AbstractRunJob {

	private List<Map<String, Object>> products;

	/** default constructor */
	public BuildCn360PriceJob(List<Map<String, Object>> products) {
		super("BuildCn360PriceJob");
		this.products = products;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.thread.batch.AbstractRunJob#execute()
	 */
	@Override
	public void execute() {
		String feedFile = ServerValue.getFrontApachePath() + "3party_ex" + File.separator + "360cn" + File.separator
				+ "yaodian100_price.csv";
		File feedFileObj = new File(feedFile);
		if (feedFileObj.exists()) {
			feedFileObj.delete();
		}
		try {
			CsvWriter writer = new CsvWriter(feedFile, ',', Charset.forName("gb2312"));
			writer.writeRecord(new String[] { "bid", "outer_id", "color", "size", "sale_price", "storage" });
			int count = 0;
			for (Map<String, Object> product : products) {
				// String spec = product.get("JOIN_SPEC").toString();
				// if (spec.indexOf(":") != -1 && spec.indexOf("|") != -1) {
				// spec = StringUtils.replace(spec, "／", ":");
				// spec = StringUtils.replace(spec, ",", "|");
				// } else {
				// spec = StringUtils.replace(spec, ":", " ");
				// spec = StringUtils.replace(spec, "|", " ");
				// spec = StringUtils.replace(spec, "／", ":");
				// spec = StringUtils.replace(spec, ",", "|");
				// }
				String color = "";
				String size = "";

				String price = product.get("SHELF_PRICE").toString();
				if (price.indexOf(".") == -1) {
					price = price + ".00";
				} else if (price.indexOf(".") == price.length() - 2) {
					price = price + "0";
				}

				writer.writeRecord(new String[] { RemoteCn360ProductService.BID, product.get("PRODUCT_ID").toString(), color,
						size, price, "1" });
				count++;
				if (count % 200 == 0) {
					writer.flush();
				}
			}
			writer.flush();
			writer.close();
		} catch (Exception e) {
			logger.error("写档错误", e);
		}
	}

}
